package work.comeback.omo.controller.servlet.user;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import work.comeback.omo.model.http.ResponseData;
import work.comeback.omo.model.user.UserBean;
import work.comeback.omo.model.user.UserBeanTypeAdapter;
import work.comeback.omo.service.UserService;
import work.comeback.omo.service.imp.UserServiceImpl;
import work.comeback.omo.util.common.AuthUtils;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserServlet", urlPatterns = "/api/user")
public class UserServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(UserServlet.class);
    private final UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResponseData data;
        String dataJson;
        Gson gson = new GsonBuilder().registerTypeAdapter(UserBean.class, new UserBeanTypeAdapter()).create();

        String action = request.getParameter("action");

        if (action == null) {
            dataJson = gson.toJson(new ResponseData());
            response.setContentType("application/json");
            response.getWriter().write(dataJson);
            return;
        }

        switch (action) {
            case "getUser":
                UserBean user = userService.findUserByUsername(
                        request.getParameter("username")
                );
                if (user == null) {
                    data = ResponseData.fault("用户不存在", null);
                } else {
                    data = ResponseData.success("success", user);
                }
                dataJson = gson.toJson(data);
                response.setContentType("application/json");
                response.getWriter().write(dataJson);
                break;
            case "list":
                data = actionUserList();
                dataJson = gson.toJson(data);
                response.setContentType("application/json");
                response.getWriter().write(dataJson);

                break;
            case "update":
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String attUrl = request.getParameter("attUrl");

                data = actionUpdate(username, password, attUrl);
                dataJson = gson.toJson(data);
                response.setContentType("application/json");
                response.getWriter().write(dataJson);
                break;

            case "delete":
                data = actionDeleteUserById(request.getParameter("username"));
                dataJson = gson.toJson(data);
                response.setContentType("application/json");
                response.getWriter().write(dataJson);
                break;
        }
    }

    private ResponseData actionUpdate(String username, String pwd, String attUrl) {
        UserBean user = userService.findUserByUsername(username);

        if (pwd != null) {
            user.setPassword(AuthUtils.encryptPassword(pwd));
        }

        if (attUrl != null) {
            user.setAvatarUrl(attUrl);
        }

        userService.updateUser(user);

        return ResponseData.success("success", null);
    }

    private ResponseData actionDeleteUserById(String username) {
        UserBean user = userService.findUserByUsername(username);

        if (user == null) {
            return ResponseData.fault("用户不存在", null);
        } else {
            userService.deleteUser(user);
            return ResponseData.success("success", null);
        }
    }

    private ResponseData actionUserList() {
        List<UserBean> userList = userService.findByRole("user");
        return ResponseData.success("success", userList);
    }
}
